﻿// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the LGPL License, Version 3.0. See License.txt in the project root for license information.
// Website: https://admin.blazor.zone

using PetaPoco;
using System;
using System.Reflection;

namespace BlazorAdmin.Services.DbService
{
    /// <summary>
    /// 
    /// </summary>
    internal class PetaPocoConventionMapper : ConventionMapper
    {

        public override Func<object, object> GetFromDbConverter(PropertyInfo targetProperty, Type sourceType) => targetProperty.PropertyType.IsEnum && sourceType == typeof(string)
            ? new Extensions.NumberToEnumConverter(targetProperty.PropertyType).ConvertFromDb
            : base.GetFromDbConverter(targetProperty, sourceType);

        public override Func<object, object> GetToDbConverter(PropertyInfo targetProperty) => targetProperty.PropertyType.IsEnum
            ? new Extensions.NumberToEnumConverter(targetProperty.PropertyType).ConvertToDb
            : base.GetToDbConverter(targetProperty);
    }
}